<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="年份" prop="year">
        <el-input
          v-model="queryParams.year"
          placeholder="请输入年份"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同名称" prop="contractName">
        <el-input
          v-model="queryParams.contractName"
          placeholder="请输入合同名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['contracts:contracts:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['contracts:contracts:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['contracts:contracts:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['contracts:contracts:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="contractsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="年份" align="center" prop="year" />
      <el-table-column label="大类" align="center" prop="majorCategory" />
      <el-table-column label="小类" align="center" prop="minorCategory" />
      <el-table-column label="合同名称" align="center" prop="contractName" />
      <el-table-column label="甲方" align="center" prop="partyA" />
      <el-table-column label="乙方" align="center" prop="partyB" />
      <el-table-column label="丙方" align="center" prop="partyC" />
      <el-table-column label="采购方式" align="center" prop="procurementMethod" />
      <el-table-column label="合同签订时间" align="center" prop="signingDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.signingDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="合同总价款(万元)" align="center" prop="totalAmount" />
      <el-table-column label="结算金额(万元)" align="center" prop="settlementAmount" />
      <el-table-column label="审计金额(万元)" align="center" prop="auditAmount" />
      <el-table-column label="支付方式" align="center" prop="paymentMethod" />
      <el-table-column label="合同编号" align="center" prop="contractNumber" />
      <el-table-column label="财务NC项目编码" align="center" prop="ncProjectCode" />
      <el-table-column label="质保金" align="center" prop="warrantyDeposit" />
      <el-table-column label="联系方式" align="center" prop="contactInfo" />
      <el-table-column label="线路" align="center" prop="line" />
      <el-table-column label="合同签订依据" align="center" prop="signingBasis" />
      <el-table-column label="累计计量(万元)" align="center" prop="cumulativeMeasurement" />
      <el-table-column label="累计计量-比例(%)" align="center" prop="measurementPercentage" />
      <el-table-column label="累计支付(万元)" align="center" prop="cumulativePayment" />
      <el-table-column label="累计支付-比例(%)" align="center" prop="paymentPercentage" />
      <el-table-column label="经办人" align="center" prop="operator" />
      <el-table-column label="是否出具法律意见书(0:否,1:是)" align="center" prop="hasLegalOpinion" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['contracts:contracts:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['contracts:contracts:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改合同信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="contractsRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="年份" prop="year">
          <el-input v-model="form.year" placeholder="请输入年份" />
        </el-form-item>
        <el-form-item label="大类" prop="majorCategory">
          <el-input v-model="form.majorCategory" placeholder="请输入大类" />
        </el-form-item>
        <el-form-item label="小类" prop="minorCategory">
          <el-input v-model="form.minorCategory" placeholder="请输入小类" />
        </el-form-item>
        <el-form-item label="合同名称" prop="contractName">
          <el-input v-model="form.contractName" placeholder="请输入合同名称" />
        </el-form-item>
        <el-form-item label="甲方" prop="partyA">
          <el-input v-model="form.partyA" placeholder="请输入甲方" />
        </el-form-item>
        <el-form-item label="乙方" prop="partyB">
          <el-input v-model="form.partyB" placeholder="请输入乙方" />
        </el-form-item>
        <el-form-item label="丙方" prop="partyC">
          <el-input v-model="form.partyC" placeholder="请输入丙方" />
        </el-form-item>
        <el-form-item label="采购方式" prop="procurementMethod">
          <el-input v-model="form.procurementMethod" placeholder="请输入采购方式" />
        </el-form-item>
        <el-form-item label="合同签订时间" prop="signingDate">
          <el-date-picker clearable
            v-model="form.signingDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择合同签订时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="合同总价款(万元)" prop="totalAmount">
          <el-input v-model="form.totalAmount" placeholder="请输入合同总价款(万元)" />
        </el-form-item>
        <el-form-item label="结算金额(万元)" prop="settlementAmount">
          <el-input v-model="form.settlementAmount" placeholder="请输入结算金额(万元)" />
        </el-form-item>
        <el-form-item label="审计金额(万元)" prop="auditAmount">
          <el-input v-model="form.auditAmount" placeholder="请输入审计金额(万元)" />
        </el-form-item>
        <el-form-item label="支付方式" prop="paymentMethod">
          <el-input v-model="form.paymentMethod" placeholder="请输入支付方式" />
        </el-form-item>
        <el-form-item label="合同编号" prop="contractNumber">
          <el-input v-model="form.contractNumber" placeholder="请输入合同编号" />
        </el-form-item>
        <el-form-item label="财务NC项目编码" prop="ncProjectCode">
          <el-input v-model="form.ncProjectCode" placeholder="请输入财务NC项目编码" />
        </el-form-item>
        <el-form-item label="质保金" prop="warrantyDeposit">
          <el-input v-model="form.warrantyDeposit" placeholder="请输入质保金" />
        </el-form-item>
        <el-form-item label="联系方式" prop="contactInfo">
          <el-input v-model="form.contactInfo" placeholder="请输入联系方式" />
        </el-form-item>
        <el-form-item label="线路" prop="line">
          <el-input v-model="form.line" placeholder="请输入线路" />
        </el-form-item>
        <el-form-item label="合同签订依据" prop="signingBasis">
          <el-input v-model="form.signingBasis" placeholder="请输入合同签订依据" />
        </el-form-item>
        <el-form-item label="累计计量(万元)" prop="cumulativeMeasurement">
          <el-input v-model="form.cumulativeMeasurement" placeholder="请输入累计计量(万元)" />
        </el-form-item>
        <el-form-item label="累计计量-比例(%)" prop="measurementPercentage">
          <el-input v-model="form.measurementPercentage" placeholder="请输入累计计量-比例(%)" />
        </el-form-item>
        <el-form-item label="累计支付(万元)" prop="cumulativePayment">
          <el-input v-model="form.cumulativePayment" placeholder="请输入累计支付(万元)" />
        </el-form-item>
        <el-form-item label="累计支付-比例(%)" prop="paymentPercentage">
          <el-input v-model="form.paymentPercentage" placeholder="请输入累计支付-比例(%)" />
        </el-form-item>
        <el-form-item label="经办人" prop="operator">
          <el-input v-model="form.operator" placeholder="请输入经办人" />
        </el-form-item>
        <el-form-item label="是否出具法律意见书(0:否,1:是)" prop="hasLegalOpinion">
          <el-input v-model="form.hasLegalOpinion" placeholder="请输入是否出具法律意见书(0:否,1:是)" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Contracts">
import { listContracts, getContracts, delContracts, addContracts, updateContracts } from "@/api/contracts/contracts";

const { proxy } = getCurrentInstance();

const contractsList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    year: null,
    contractName: null,
  },
  rules: {
    year: [
      { required: true, message: "年份不能为空", trigger: "blur" }
    ],
    majorCategory: [
      { required: true, message: "大类不能为空", trigger: "blur" }
    ],
    minorCategory: [
      { required: true, message: "小类不能为空", trigger: "blur" }
    ],
    contractName: [
      { required: true, message: "合同名称不能为空", trigger: "blur" }
    ],
    partyA: [
      { required: true, message: "甲方不能为空", trigger: "blur" }
    ],
    partyB: [
      { required: true, message: "乙方不能为空", trigger: "blur" }
    ],
    partyC: [
      { required: true, message: "丙方不能为空", trigger: "blur" }
    ],
    procurementMethod: [
      { required: true, message: "采购方式不能为空", trigger: "blur" }
    ],
    signingDate: [
      { required: true, message: "合同签订时间不能为空", trigger: "blur" }
    ],
    totalAmount: [
      { required: true, message: "合同总价款(万元)不能为空", trigger: "blur" }
    ],
    settlementAmount: [
      { required: true, message: "结算金额(万元)不能为空", trigger: "blur" }
    ],
    auditAmount: [
      { required: true, message: "审计金额(万元)不能为空", trigger: "blur" }
    ],
    paymentMethod: [
      { required: true, message: "支付方式不能为空", trigger: "blur" }
    ],
    contractNumber: [
      { required: true, message: "合同编号不能为空", trigger: "blur" }
    ],
    ncProjectCode: [
      { required: true, message: "财务NC项目编码不能为空", trigger: "blur" }
    ],
    warrantyDeposit: [
      { required: true, message: "质保金不能为空", trigger: "blur" }
    ],
    contactInfo: [
      { required: true, message: "联系方式不能为空", trigger: "blur" }
    ],
    line: [
      { required: true, message: "线路不能为空", trigger: "blur" }
    ],
    signingBasis: [
      { required: true, message: "合同签订依据不能为空", trigger: "blur" }
    ],
    cumulativeMeasurement: [
      { required: true, message: "累计计量(万元)不能为空", trigger: "blur" }
    ],
    measurementPercentage: [
      { required: true, message: "累计计量-比例(%)不能为空", trigger: "blur" }
    ],
    cumulativePayment: [
      { required: true, message: "累计支付(万元)不能为空", trigger: "blur" }
    ],
    paymentPercentage: [
      { required: true, message: "累计支付-比例(%)不能为空", trigger: "blur" }
    ],
    operator: [
      { required: true, message: "经办人不能为空", trigger: "blur" }
    ],
    hasLegalOpinion: [
      { required: true, message: "是否出具法律意见书(0:否,1:是)不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询合同信息列表 */
function getList() {
  loading.value = true;
  listContracts(queryParams.value).then(response => {
    contractsList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    year: null,
    majorCategory: null,
    minorCategory: null,
    contractName: null,
    partyA: null,
    partyB: null,
    partyC: null,
    procurementMethod: null,
    signingDate: null,
    totalAmount: null,
    settlementAmount: null,
    auditAmount: null,
    paymentMethod: null,
    contractNumber: null,
    ncProjectCode: null,
    warrantyDeposit: null,
    contactInfo: null,
    line: null,
    signingBasis: null,
    cumulativeMeasurement: null,
    measurementPercentage: null,
    cumulativePayment: null,
    paymentPercentage: null,
    operator: null,
    hasLegalOpinion: null,
    createdAt: null,
    updatedAt: null
  };
  proxy.resetForm("contractsRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加合同信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getContracts(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改合同信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["contractsRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateContracts(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addContracts(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除合同信息编号为"' + _ids + '"的数据项？').then(function() {
    return delContracts(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('contracts/contracts/export', {
    ...queryParams.value
  }, `contracts_${new Date().getTime()}.xlsx`)
}

getList();
</script>
